package com.cndw.kungfu.model;

import java.util.Collection;
import java.util.Iterator;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentLinkedQueue;
import java.util.concurrent.ConcurrentSkipListSet;

import com.cndw.kungfu.model.player.domain.FriendAward;

public class DataStaticCenter {

	/**
	 * 好友升级的祝福信息
	 */
	private static ConcurrentLinkedQueue<FriendAward> friendAwards = new ConcurrentLinkedQueue<FriendAward>();

	private static ConcurrentHashMap<Long, Integer> friendFinds = new ConcurrentHashMap<Long, Integer>();
	/**
	 * 跨服对决打开窗口的玩家
	 */
	private static ConcurrentHashMap<Long, Boolean> campCrossOpenIds = new ConcurrentHashMap<Long, Boolean>();
	/**
	 * 打开视频窗口的玩家
	 */
	private static ConcurrentHashMap<Long, Boolean> openMJIds = new ConcurrentHashMap<Long, Boolean>();
	/**
	 * 送花可kiss次数
	 */
	private static ConcurrentHashMap<Long, Integer> marryFlowerKiss = new ConcurrentHashMap<Long, Integer>();
	/**
	 * 申请结婚
	 */
	private static ConcurrentHashMap<Long, Long> marryApplyIds = new ConcurrentHashMap<Long, Long>();
	/**
	 * 结婚的申请等级
	 */
	private static ConcurrentHashMap<Long, Integer> marryTypes = new ConcurrentHashMap<Long, Integer>();
	/**
	 * 结婚的祝福对象
	 */
	private static ConcurrentHashMap<Long, ConcurrentSkipListSet<Long>> marryZFIds = new ConcurrentHashMap<Long, ConcurrentSkipListSet<Long>>();

	private static ConcurrentHashMap<Long, ConcurrentSkipListSet<Long>> marryGiftsIds = new ConcurrentHashMap<Long, ConcurrentSkipListSet<Long>>();

	public static void friendAwardAdd(long s, long t, int l) {
		FriendAward friendAward = new FriendAward(s, t, l);
		friendAwards.add(friendAward);

	}

	public static boolean friendAwardOne(long s, long uid, int level, int o) {

		Iterator<FriendAward> it = friendAwards.iterator();
		while (it.hasNext()) {
			FriendAward friendAward = (FriendAward) it.next();
			if (friendAward.getS() == s && friendAward.getT() == uid && friendAward.getL() == level) {
				if (friendAward.getO() == o) {
					friendAward.setO(o + 1);
					if (o == 1) {
						it.remove();
					}
					return true;
				} else {
					return false;
				}
			}

		}

		return false;
	}

	public static int getLeftFind(long uid) {
		return friendFinds.containsKey(uid) ? friendFinds.get(uid) : 0;
	}

	public static int leftFindAlter(long uid, int alter) {
		int left = getLeftFind(uid);
		left += alter;
		if (left <= 0) {
			friendFinds.remove(uid);
		} else {
			friendFinds.put(uid, left);
		}
		return left;
	}

	public static void campCrossOpen(long uid, boolean open) {
		if (open) {
			campCrossOpenIds.put(uid, true);
		} else {
			campCrossOpenIds.remove(uid);
		}
	}

	public static Collection<Long> getCampCrossIds() {
		return campCrossOpenIds.keySet();
	}

	public static boolean campCrossOpen(long uid) {
		return campCrossOpenIds.containsKey(uid);
	}

	public static void campCrossState(boolean open) {
		campCrossOpenIds.clear();
	}

	public static void flashMJOpen(long uid, boolean open) {
		if (open) {
			openMJIds.put(uid, true);
		} else {
			openMJIds.remove(uid);
		}
	}

	public static int flashMJOnLineNum() {
		return openMJIds.size();
	}

	public static boolean marryFlowerAdd(long uid, int alter) {
		int num = marryFlowerKissGet(uid);
		num += alter;
		if (num < 0) {
			return false;
		} else if (num == 0) {
			marryFlowerKiss.remove(uid);
		} else {
			marryFlowerKiss.put(uid, num);
		}
		return true;
	}

	private static int marryFlowerKissGet(long uid) {
		return marryFlowerKiss.containsKey(uid) ? marryFlowerKiss.get(uid) : 0;
	}

	public static void marryApply(long uid, long targetId, int type) {
		marryApplyIds.put(uid, targetId);
		marryTypes.put(uid, type);
	}

	public static int marryCheck(long manId, long womanId) {
		boolean ok = marryApplyIds.containsKey(manId) && marryApplyIds.get(manId) == womanId;
		if (ok) {
			return marryTypes.get(manId);
		}
		return -1;
	}

	public static Set<Long> marryZFIds(long uid) {
		Set<Long> onLineIds = Context.getTcpServer().allOnLineIds();
		marryZFIds.put(uid, new ConcurrentSkipListSet<Long>(onLineIds));
		marryGiftsIds.put(uid, new ConcurrentSkipListSet<Long>(onLineIds));
		return onLineIds;
	}

	public static boolean marryZFOk(long manId, long uid) {
		return marryZFIds.containsKey(manId) && marryZFIds.get(manId).remove(uid);
	}

	public static boolean marryGiftOk(long manId, long uid) {
		return marryGiftsIds.containsKey(manId) && marryGiftsIds.get(manId).remove(uid);
	}

	public static boolean marryGiftOk(long manId) {
		return marryGiftsIds.containsKey(manId);
	}

}
